#include "gtkmain.h"
#include "gtkprefs.h"


Gtk::Window *ghghasd;

GtkMain::GtkMain() {
    cout << "asd" << endl;
    Glib::thread_init(NULL);

    int argc = 0;
    char **argv = NULL;
    kit = new Gtk::Main(argc, argv);
    
    try {
        ref_xml = Gnome::Glade::Xml::create("../resources/main.glade");
    } catch (const Gnome::Glade::XmlError& ex) {
        std::cerr << ex.what() << std::endl;
        exit(1);
    }

    //Gtk::RC("resources/gtkrc-2.0");

    //Gdk::Screen *scr = new Gdk::Screen::get_default();
    Glib::RefPtr<Gdk::Screen> scr = Gdk::Screen::get_default();
    //cout << scr->get_width() << endl;


    ref_xml->get_widget("MainWindow", MainWindow);
    if (MainWindow) {
        Gtk::ToolButton *tmp_tb;
        
        MainWindow->set_size_request(scr->get_width(), scr->get_height());

        ref_xml->get_widget("TbQuit", tmp_tb);
        if (tmp_tb) {
            tmp_tb->signal_clicked().connect(
                    sigc::mem_fun(*this, &GtkMain::TbQuit_Clicked));
        }

        ref_xml->get_widget("TbPreferences", tmp_tb);
        if (tmp_tb) {
            tmp_tb->signal_clicked().connect(
                    sigc::mem_fun(*this, &GtkMain::TbPreferences_Clicked));
        }

        ref_xml->get_widget("TbFullscreen", tmp_tb);
        if (tmp_tb) {
            tmp_tb->signal_clicked().connect(
                    sigc::mem_fun(*this, &GtkMain::TbFullscreen_Clicked));
        }


        gst_init(&argc, &argv);

        GstViewer *av1 = new GstViewer("http://localhost:4481");
        GstViewer *av2 = new GstViewer("http://localhost:4482");
        GstViewer *av3 = new GstViewer("http://localhost:4483");
        GstViewer *av4 = new GstViewer("http://localhost:4484");

        /*GstViewer *av1 = new GstViewer("v4l://video5");
        GstViewer *av2 = new GstViewer("v4l://video7");
        GstViewer *av3 = new GstViewer("v4l://video9");
        GstViewer *av4 = new GstViewer("v4l://video11");
        GstViewer *av5 = new GstViewer("v4l://video13");
        GstViewer *av6 = new GstViewer("v4l://video15");
        GstViewer *av7 = new GstViewer("v4l://video17");
        GstViewer *av8 = new GstViewer("v4l://video19");*/

        ref_xml->get_widget("table1", table1);
        /*table1->attach(*av1, 0, 1, 0, 1, Gtk::EXPAND, Gtk::EXPAND);
        table1->attach(*av2, 1, 2, 0, 1, Gtk::EXPAND, Gtk::EXPAND);
        table1->attach(*av3, 2, 3, 0, 1, Gtk::EXPAND, Gtk::EXPAND);
        table1->attach(*av4, 0, 1, 1, 2, Gtk::EXPAND, Gtk::EXPAND);
        table1->attach(*av5, 1, 2, 1, 2, Gtk::EXPAND, Gtk::EXPAND);
        table1->attach(*av6, 2, 3, 1, 2, Gtk::EXPAND, Gtk::EXPAND);
        table1->attach(*av7, 0, 1, 2, 3, Gtk::EXPAND, Gtk::EXPAND);
        table1->attach(*av8, 1, 2, 2, 3, Gtk::EXPAND, Gtk::EXPAND);*/

        table1->attach(*av1, 0, 1, 0, 1, Gtk::EXPAND, Gtk::EXPAND);
        table1->attach(*av2, 0, 1, 1, 2, Gtk::EXPAND, Gtk::EXPAND);
        table1->attach(*av3, 1, 2, 0, 1, Gtk::EXPAND, Gtk::EXPAND);
        table1->attach(*av4, 1, 2, 1, 2, Gtk::EXPAND, Gtk::EXPAND);

        //table1->attach(*av9, 2, 3, 0, 1, Gtk::EXPAND, Gtk::EXPAND);
        //av1->show();
        Glib::signal_timeout().connect(sigc::mem_fun(*this, &GtkMain::timer_callback), 100);
        GtkPrefs(this->ref_xml);

        //hack da levare start
        //this->MainWindow->fullscreen();
        ghghasd = this->MainWindow;
        //hack da levare end

        kit->run(*this->MainWindow);
    }
}

bool GtkMain::timer_callback() {
    MainWindow->queue_resize();
    //MainWindow->queue_resize_no_redraw();
    //cout << "timer" << endl;
    return 1;
}

void GtkMain::TbPreferences_Clicked() {

}

void GtkMain::TbFullscreen_Clicked() {
    static bool fullscreen; //this is an hack:
    if (!fullscreen) { //the proper way is to use the window_state_event
        this->MainWindow->fullscreen();
        fullscreen = true;
    } else {
        this->MainWindow->unfullscreen();
        fullscreen = false;
    }
}

void GtkMain::TbQuit_Clicked() {
    cout << "quit" << endl;
    if (MainWindow)
        MainWindow->hide(); //hide() will cause main::run() to end.
}
